package com.lkd.job;

import com.lkd.entity.UserEntity;
import com.lkd.service.UserService;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.util.List;

@Component
public class Userjob {

    @Autowired
    private UserService userService;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 员工工单列表每日初始化
     * @param param
     * @return
     * @throws Exception
     */
    @XxlJob("workCountInitJobHandler")
    public ReturnT<String> workCountInitJobHandler(String param) throws Exception{
        //查询用户列表
        List<UserEntity> userEntities = userService.list();
        //构建数据（zset）

        int plusday = 1;
        if("today".equals(param)){
            plusday = 0;
        }
        String date = LocalDate.now().plusDays(plusday).toString();
        for (UserEntity userEntity : userEntities) {
            //构建大key,前缀+日期+区域+工单类别
            String key ="workCount"+date+userEntity.getRegionId()+userEntity.getRoleCode();
            //存入redis
            redisTemplate.opsForZSet().add(key,userEntity.getId(),0);
        }

        return ReturnT.SUCCESS;
    }

}
